package com.cby.library.widget.alpha;

import android.content.Context;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatButton;

/**
 * 在 pressed 和 disabled 时改变 View 的透明度
 *
 * @author 徐智伟
 * @create 2018/7/27
 */
public class AlphaButton extends AppCompatButton {

  private AlphaViewHelper mAlphaViewHelper;

  public AlphaButton(Context context) {
    super(context);
  }

  public AlphaButton(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public AlphaButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  private AlphaViewHelper getAlphaViewHelper() {
    if (mAlphaViewHelper == null) {
      mAlphaViewHelper = new AlphaViewHelper(this, 0.8f, 0.5f);
    }
    return mAlphaViewHelper;
  }

  @Override
  public void setPressed(boolean pressed) {
    super.setPressed(pressed);
    getAlphaViewHelper().onPressedChanged(this, pressed);
  }

  @Override
  public void setEnabled(boolean enabled) {
    super.setEnabled(enabled);
    getAlphaViewHelper().onEnabledChanged(this,enabled);
  }


  /**
   * 设置是否要在 press 时改变透明度
   *
   * @param changeAlphaWhenPress 是否要在 press 时改变透明度
   */
  public void setChangeAlphaWhenPress(boolean changeAlphaWhenPress) {
    getAlphaViewHelper().setChangeAlphaWhenPress(changeAlphaWhenPress);
  }

  /**
   * 设置是否要在 disabled 时改变透明度
   *
   * @param changeAlphaWhenDisable 是否要在 disabled 时改变透明度
   */
  public void setChangeAlphaWhenDisable(boolean changeAlphaWhenDisable) {
    getAlphaViewHelper().setChangeAlphaWhenDisable(changeAlphaWhenDisable);
  }

}
